package main

import (
	"context"
	"fmt"
	"gitee.com/DonHz/electronic-business-systems/mxshop_srvs/order_srv/proto"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

var (
	orderClient proto.OrderClient
	conn        *grpc.ClientConn
)

func Init() {
	var err error
	conn, err = grpc.NewClient("127.0.0.1:57626", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic("create client connect failed: " + err.Error())
	}
	orderClient = proto.NewOrderClient(conn)
}

func main() {
	Init()
	defer conn.Close()
	//TestCreateCartItem()
	//TestCartItemList()
	//TestUpdateCartItem()
	//TestCreateOrder()
	//TestOrderDetail()
	TestOrderList()
}

func TestCreateCartItem() {
	resp, err := orderClient.CreateCartItem(context.Background(), &proto.CartItemRequest{
		UserId:  11,
		GoodsId: 422,
		Nums:    5,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("create cart item success: userId: ", resp.UserId)
}

func TestCartItemList() {
	resp, err := orderClient.CartItemList(context.Background(), &proto.UserInfo{
		Id: 11,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("cart item list success: total: ", resp.Total)
	for _, item := range resp.Data {
		fmt.Println("cart item: userId: ", item.UserId, " goodsId: ", item.GoodsId, " nums: ", item.Nums, " checked: ", item.Checked)
	}
}

func TestUpdateCartItem() {
	_, err := orderClient.UpdateCartItem(context.Background(), &proto.CartItemRequest{
		Id:      1,
		Checked: true,
	})
	if err != nil {
		panic(err)
	}
}

func TestCreateOrder() {
	resp, err := orderClient.CreateOrder(context.Background(), &proto.OrderRequest{
		UserId:  11,
		Address: "北京12",
		Name:    "张三12",
		Mobile:  "13800138111",
		Post:    "尽快发货111",
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("create order success: orderId: ", resp.Id)
}

func TestOrderDetail() {
	resp, err := orderClient.OrderDetail(context.Background(), &proto.OrderRequest{
		Id:     1,
		UserId: 11,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("order detail success: orderSn: ", resp.OrderInfo.OrderSn)
	for _, item := range resp.Goods {
		fmt.Println("order goods: goodsId: ", item.GoodsId, " goodsName: ", item.GoodsName, " goodsImage: ", item.GoodsImage, " goodsPrice: ", item.GoodsPrice, " nums: ", item.Nums)
	}
}

func TestOrderList() {
	resp, err := orderClient.OrderList(context.Background(), &proto.OrderFilterRequest{
		UserId: 11,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("order list success: total: ", resp.Total)
	for _, item := range resp.Data {
		fmt.Println(item)
	}
}
